﻿using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace GpCommon.Helpers
{
    public class UrlHelper
    {
        #region 网址
        /// <summary>
        /// 获取全部实时重点数据(type  1  涨速   2  涨幅)
        /// </summary>
        /// <returns></returns>
        public static List<string> GetUrlForAllRealTimeMainData(int orderType, int pn = 0, int count = 0)
        {
            List<string> urls = new List<string>();
            int fid = 22;
            switch (orderType)
            {
                // 涨速排行
                case 1:
                    fid = 22;
                    break;
                // 涨幅排行
                case 2:
                    fid = 3;
                    break;
            }
            //int count = gpCount == 0 ? CommonData.TotalGpCount : gpCount;
            if(pn == 0)
            {
                int pz = 100;
                int tempCount = CommonData.TotalGpCount;
                if (count != 0)
                    tempCount = count;
                if (count > 0 && count < 100)
                    pz = count;
                var gpCount = tempCount / 100 + (tempCount % 100 > 0 ? 1 : 0);
                for (int i = 1; i <= gpCount; i++)
                {
                    urls.Add($"http://74.push2.eastmoney.com/api/qt/clist/get?pn={i}&pz={pz}" +
                                $"&po=1&np=1&fltt=2&invt=2&wbp2u=|0|0|0|web" +
                                $"&fid=f{fid}" +
                                $"&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048&fields=f2,f3,f12,f14,f20,f21,f22,f11");
                }
            }
            else
            {
                urls.Add($"http://74.push2.eastmoney.com/api/qt/clist/get?pn={pn}&pz=100" +
                            $"&po=1&np=1&fltt=2&invt=2&wbp2u=|0|0|0|web" +
                            $"&fid=f{fid}" +
                            $"&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048&fields=f2,f3,f12,f14,f20,f21,f22,f11");
            }
            return urls;
        }
        /// <summary>
        /// 获取全部实时数据
        /// </summary>
        /// <returns></returns>
        public static List<string> GetUrlForAllRealTimeData()
        {
            List<string> urls = new List<string>();

            var count = CommonData.TotalGpCount / 100;
            count += CommonData.TotalGpCount % 100 > 0 ? 1 : 0;
            for (int i = 1; i <= count; i++)
            {
                urls.Add($"https://42.push2.eastmoney.com/api/qt/clist/get?pn={i}&pz=100&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&dect=1&wbp2u=|0|0|0|web&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152");
            }
            return urls;        
        }

        /// <summary>
        /// 按条件获取沪深主板股票实时数据
        /// </summary>
        /// <param name="minPrice"></param>
        /// <param name="maxPrice"></param>
        /// <param name="minRate"></param>
        /// <param name="maxRate"></param>
        /// <param name="ps"></param>
        /// <returns></returns>
        public static string GetUrlForAllRealTimeDataByFilter(float minPrice, float maxPrice, float minRate, float maxRate, int ps)
        {
            return $" https://data.eastmoney.com/dataapi/xuangu/list?st=CHANGE_RATE&sr=-1" +
                $"&ps={ps}" +
                $"&p=1&sty=2CSECURITY_CODE%2CSECURITY_NAME_ABBR%2CNEW_PRICE%2CCHANGE_RATE%2CHIGH_PRICE%2CLOW_PRICE%2CPRE_CLOSE_PRICE%2CNEW_PRICE%2CCHANGE_RATE" +
                $"&filter=(NEW_PRICE%3E%3D{minPrice})(NEW_PRICE%3C%3D{maxPrice})(CHANGE_RATE%3E%3D{minRate})(CHANGE_RATE%3C%3D{maxRate})" +
                //$"(MARKET+in+(%22%E4%B8%8A%E4%BA%A4%E6%89%80%E4%B8%BB%E6%9D%BF%22%2C%22%E6%B7%B1%E4%BA%A4%E6%89%80%E4%B8%BB%E6%9D%BF%22))" +
                $"&source=SELECT_SECURITIES&client=WEB";
        }
        /// <summary>
        /// 获取指定股票实时数据
        /// </summary>
        /// <returns></returns>
        public static string GetUrlForSingleRealTimeData(string code)
        {
            int typeId = 0;
            if (code.StartsWith("00"))
            {
                typeId = 0;
            }
            else if (code.StartsWith("60"))
            {
                typeId = 1;
            }
            else
            {
                return "";
            }
            return $"https://15.push2.eastmoney.com/api/qt/stock/get?fltt=2&invt=2&volt=2&fields=f43,f57,f58,f169,f170,f46,f44,f45&secid={typeId}.{code}";

        }
        /// <summary>
        /// 获取当日涨停股
        /// </summary>
        /// <param name="time"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string GetUrlForTodayTopGps(DateTime time, int count)
        {
            string day = time.Year + "" + time.Month.ToString("D2") + time.Day.ToString("D2");
            return $"http://push2ex.eastmoney.com/getTopicZTPool?ut=7eea3edcaed734bea9cbfc24409ed989&dpt=wz.ztzt&Pageindex=0&pagesize={count}&sort=fbt%3Aasc&date={day}";
        }
        /// <summary>
        /// 获取昨日涨停股
        /// </summary>
        /// <param name="time"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string GetUrlForYesTodayTopGps(DateTime time, int count)
        {
            string day = time.Year + "" + time.Month.ToString("D2") + time.Day.ToString("D2");
            return $"http://push2ex.eastmoney.com/getYesterdayZTPool?ut=7eea3edcaed734bea9cbfc24409ed989&dpt=wz.ztzt&Pageindex=0&pagesize={count}&sort=zs%3Aasc&date={day}";
        }
        /// <summary>
        /// 获取指定股票历史数据
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetUrlForSingleHistoryData(string code)
        {
            int typeId = 0;
            if (code.StartsWith("00"))
            {
                typeId = 0;
            }
            else if (code.StartsWith("60"))
            {
                typeId = 1;
            }
            else
            {
                return "";
            }
            return string.Format("http://push2his.eastmoney.com/api/qt/stock/kline/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&beg=0&end=20500101&rtntype=6&fqt=0&secid={0}.{1}&klt=101&fqt=1", typeId, code);

        }
        /// <summary>
        /// 获取指定股票分时数据
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetUrlForSingleDetailData(string code)
        {
            int typeId = 0;
            if (code.StartsWith("00"))
            {
                typeId = 0;
            }
            else if (code.StartsWith("60"))
            {
                typeId = 1;
            }
            return $"https://push2.eastmoney.com/api/qt/stock/trends2/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58&iscr=0&ndays=1&secid={typeId}.{code}";

        }

        /// <summary>
        /// 获取指定股票东方财富页面
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetUrlForSingleGpBrowenPage(string code)
        {
            string typeId = "";
            if (code.StartsWith("00"))
            {
                typeId = "sz";
            }
            else if (code.StartsWith("60"))
            {
                typeId = "sh";
            }
            return $"https://quote.eastmoney.com/{typeId}{code}.html";
        }
        /// <summary>
        /// 获取指定股票指定时间分时网页
        /// </summary>
        /// <param name="code"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string GetUrlForSingleGpDetailDataBrowenPage(string code, string time)
        {
            string typeId = "";
            if (code.StartsWith("00"))
            {
                typeId = "sz";
            }
            else if (code.StartsWith("60"))
            {
                typeId = "sh";
            }

            return $"https://finance.sina.com.cn/h5charts/tchart.html?symbol={typeId}{code}&date={time}&rangeselector=true&indicator=tvol";
        }

        /// <summary>
        /// 获取股票历史分时数据
        /// </summary>
        /// <param name="code"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string GetUrlForSingleGpHistoryDetailData(string code, DateTime time)
        {
            string typeId = "";
            if (code.StartsWith("00"))
            {
                typeId = "SZ";
            }
            else if (code.StartsWith("60"))
            {
                typeId = "SS";
            }
            string day = time.Year + "" + time.Month.ToString("D2") + time.Day.ToString("D2");
            return $"http://zzw.hsmdb.com/iwin_zzbweb-webapp/quote/v1/trend?prod_code={code}.{typeId}&fields=last_px,business_amount,avg_px&date={day}";
        }

        /// <summary>
        /// 获取日线图片
        /// </summary>
        /// <param name="code"></param>
        /// <param name="formula"></param>
        /// <returns></returns>
        public static string GetUrlForDayLineImage(string code, int formula)
        {
            int type = 0;
            if (code.StartsWith("60"))
                type = 1;
            string formulaStr = "MACD";
            if (formula == 1)
                formulaStr = "KDJ";
            return $"https://webquoteklinepic.eastmoney.com/GetPic.aspx?nid={type}.{code}&unitWidth=-6&imageType=KXL&formula={formulaStr}";
        }

        /// <summary>
        /// 获取分时图片(带盘前)  Type rc  不带  r
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetUrlForHourLineImage(string code)
        {
            int type = 0;
            if (code.StartsWith("60"))
                type = 1;
            return $"https://webquotepic.eastmoney.com/GetPic.aspx?imageType=rc&nid={type}.{code}";
        }

        /// <summary>
        /// 获取个股所属行业
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetUrlForGpSectorInfo(string code)
        {
            int typeId = 0;
            if (code.StartsWith("00"))
            {
                typeId = 0;
            }
            else if (code.StartsWith("60"))
            {
                typeId = 1;
            }
            return $"https://push2.eastmoney.com/api/qt/slist/get?fltt=1&invt=2&fields=f14%2Cf12%2Cf128%2Cf140&secid={typeId}.{code}&pi=0&po=1&np=1&pz=1&spt=3";
        }

        /// <summary>
        /// 获取行业排名
        /// </summary>
        /// <returns></returns>
        public static string GetUrlForSectorRank()
        {
            return $"https://23.push2.eastmoney.com/api/qt/clist/get?pn=1&pz={CommonData.TotalGpSectorCount}&po=1&np=1&fltt=2&invt=2&dect=1&wbp2u=|0|0|0|web&fid=f3&fs=m:90+t:2+f:!50&fields=f12,f14";
        }

        /// <summary>
        /// 获取个股当日详细信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetUrlForGpDetailInfo(string code)
        {
            int typeId = 0;
            if (code.StartsWith("00"))
            {
                typeId = 0;
            }
            else if (code.StartsWith("60"))
            {
                typeId = 1;
            }
            return $"https://push2.eastmoney.com/api/qt/stock/get?invt=2&fltt=1&fields=f58%2Cf734%2Cf107%2Cf57%2Cf43%2Cf59%2Cf169%2Cf301%2Cf60%2Cf170%2Cf152%2Cf177%2Cf111%2Cf46%2Cf44%2Cf45%2Cf47%2Cf260%2Cf48%2Cf261%2Cf279%2Cf277%2Cf278%2Cf288%2Cf19%2Cf17%2Cf531%2Cf15%2Cf13%2Cf11%2Cf20%2Cf18%2Cf16%2Cf14%2Cf12%2Cf39%2Cf37%2Cf35%2Cf33%2Cf31%2Cf40%2Cf38%2Cf36%2Cf34%2Cf32%2Cf211%2Cf212%2Cf213%2Cf214%2Cf215%2Cf210%2Cf209%2Cf208%2Cf207%2Cf206%2Cf161%2Cf49%2Cf171%2Cf50%2Cf86%2Cf84%2Cf85%2Cf168%2Cf108%2Cf116%2Cf167%2Cf164%2Cf162%2Cf163%2Cf92%2Cf71%2Cf117%2Cf292%2Cf51%2Cf52%2Cf191%2Cf192%2Cf262%2Cf294%2Cf295%2Cf269%2Cf270%2Cf256%2Cf257%2Cf285%2Cf286%2Cf748%2Cf747&secid={typeId}.{code}";
        
        }

        /// <summary>
        /// 获取指定股票分时数据（带盘前）
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetUrlForSingleDetailDataWithPre(string code)
        {
            int typeId = 0;
            if (code.StartsWith("00"))
            {
                typeId = 0;
            }
            else if (code.StartsWith("60"))
            {
                typeId = 1;
            }
            return $"https://push2his.eastmoney.com/api/qt/stock/trends2/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f52,f53,f54,f55,f56,f57,f58&ndays=1&iscr=1&iscca=0&secid={typeId}.{code}";
        }

        /// <summary>
        ///  获取概念板块排名数据
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string GetUrlForNotionRank(int count, int pg)
        {
            return $"https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m%3A90%2Bt%3A3%2Bf%3A!50&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf20%2Cf8%2Cf104%2Cf105%2Cf128%2Cf140%2Cf141%2Cf207%2Cf208%2Cf209%2Cf136%2Cf222&fid=f3&pn={pg}&pz={count}&po=1&dect=1";
        }

        /// <summary>
        /// 根据概念板块代号获取板块成分股数据
        /// </summary>
        /// <param name="code"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string GetUrlForNotionMembersByCode(string code, int count)
        {
            return $"https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=b%3A{code}%2Bf%3A!50&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf5%2Cf6%2Cf7%2Cf15%2Cf18%2Cf16%2Cf17%2Cf10%2Cf8%2Cf9%2Cf23&fid=f3&pn=1&pz={count}&po=1&dect=1";
        }


        /// <summary>
        /// 根据股票代号获取股票概念
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetUrlForGpNotionByCode(string code)
        {
            string typeId = "";
            if (code.StartsWith("00"))
            {
                typeId = "SZ";
            }
            else if (code.StartsWith("60"))
            {
                typeId = "SH";
            }
            return $"https://datacenter.eastmoney.com/securities/api/data/v1/get?reportName=RPT_F10_CORETHEME_BOARDTYPE&columns=SECUCODE%2CSECURITY_CODE%2CSECURITY_NAME_ABBR%2CNEW_BOARD_CODE%2CBOARD_NAME%2CSELECTED_BOARD_REASON%2CIS_PRECISE%2CBOARD_RANK%2CBOARD_YIELD%2CDERIVE_BOARD_CODE&quoteColumns=f3~05~NEW_BOARD_CODE~BOARD_YIELD&filter=(SECUCODE%3D%22{code}.{typeId}%22)(IS_PRECISE%3D%221%22)&pageNumber=1&pageSize=&sortTypes=1&sortColumns=BOARD_RANK&source=HSF10&client=PC";
        }
        #endregion
    }
}
